package doyouknow.group.CnAndZhb.controller;

import doyouknow.group.CnAndZhb.controller.session.MySessionAttributes;
import doyouknow.group.CnAndZhb.entity.TbAdmin;
import doyouknow.group.CnAndZhb.error.BusinessException;
import doyouknow.group.CnAndZhb.error.EnumBusinessError;
import doyouknow.group.CnAndZhb.response.BaseControllerReturn;
import doyouknow.group.CnAndZhb.service.TbAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import doyouknow.group.CnAndZhb.utils.SerializedField;

import javax.annotation.Resource;

/**
 * (TbAdmin)表控制层
 *
 * @author cn
 * @since 2020-11-03 21:05:02
 */
@RestController("admin1")
@RequestMapping("tbAdmin")
public class TbAdminController extends BaseController{
    /**
     * 服务对象
     */
    @Autowired
    private TbAdminService tbAdminService;

    @Autowired
    private HttpServletRequest httpServletRequest;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public TbAdmin selectOne(Integer id) {
        return this.tbAdminService.queryById(id);
    }

    @RequestMapping("/login")
    @SerializedField(includes = {"id","username","usertoken"})
    public Object Login(@RequestParam(defaultValue="") String username,@RequestParam(defaultValue="") String password)throws BusinessException
    {

        //入参校验
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR,"请输入正确的用户名或密码");
        }

        //登录服务
        TbAdmin tbAdmin =  tbAdminService.validateAdminLogin(username,password,1);
        this.httpServletRequest.getSession().setAttribute(String.valueOf(MySessionAttributes.IS_LOG),true);
        this.httpServletRequest.getSession().setAttribute(String.valueOf(MySessionAttributes.LOGIN_USER),tbAdmin);

        return BaseControllerReturn.create(tbAdmin,null);


    }

}